package net.xju_11_12.mapper;

import net.xju_11_12.entity.Card;
import net.xju_11_12.entity.Clazz;
import net.xju_11_12.entity.Student;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface StudentMapper {

    // 查询
    @Select(" select * from newtable")
    List<Student> selectAll();

    // 新增
    @Insert(" insert into newtable(sn,name) values(#{sn},#{name})")
    Integer insert(Student student);

    Integer update(Student student);

    Integer delete(int i);

    List<Student> selectBySn(String sn);

    Student selectBySnAndName(@Param("sn") String sn,@Param("name") String name);

    Student selectByMap(HashMap<String, Object> map);

    @MapKey("id")
    Map<String,Object> selectByName(String name);

//    @Select("select * from  newtable n join card  c on n.card_id = c.id  where n.id = #{id}")
//    @Results({
//            @Result(property = "id",column = "id",id = true),
//            @Result(property = "sn",column = "sn"),
//            @Result(property = "name",column = "name"),
//            @Result(property = "cardId",column = "card_id"),
//            @Result(property = "clazzId",column = "clazz_id"),
//            @Result(property = "card.id",column = "id"),
//            @Result(property = "card.balance",column = "balance")
//    })
    @Select("select * from  newtable where id  = #{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "sn",column = "sn"),
            @Result(property = "name",column = "name"),
            @Result(property = "cardId",column = "card_id"),
            @Result(property = "clazzId",column = "clazz_id"),
            @Result(property = "card",
            column = "card_id",
            javaType = Card.class,
            one = @One(select = "net.xju_11_12.mapper.StudentMapper.selectCardById"))
    })
    Student selectById(int id);
    @Select("select * from card where id = #{id}")
    Card selectCardById(int id);

    @Select("select  * from clazz  where id = #{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "clazzName",column = "clazz_name"),
            @Result(property = "sum",column = "sum"),
            @Result(property = "studentList",
            column = "id",
            javaType = List.class,
            many = @Many(select = "net.xju_11_12.mapper.StudentMapper.selectByCid"))
    })
    Clazz selectByClazzId(int clazzId);

    @Select("select  * from  newtable where clazz_id =#{id}")
    List<Student> selectByCid(int id);

    List<Student> selectByIds(@Param("ids") List<Integer> ids);

    Integer batchInsert(List<Student> students);
}
